using System;
using Fornax020.SerialComm.Core;
using Fornax020.SerialComm.Protocol;

namespace Fornax020.SerialComm.Commands
{
    /// <summary>
    /// KV设置命令 - 命令模式的具体实现
    /// </summary>
    public class SetKvCommand : CommandBase
    {
        private readonly double _kvValue;

        public SetKvCommand(double kvValue)
        {
            _kvValue = kvValue;
            Name = "SetKV";
            Description = $"设置KV值为 {kvValue}";
        }

        public override byte[] GetCommandData()
        {
            // 根据协议生成VP设置命令
            // 格式: STX + 'V' + 'P' + 数值 + CR
            byte[] command = new byte[8];
            command[0] = (byte)ProtocolConstants.STX;  // 起始字节
            command[1] = (byte)'V';  // VP指令
            command[2] = (byte)'P';  // VP指令
            
            // 将KV值转换为4位数字字符串
            int kvRaw = (int)(_kvValue * 10); // 转换为整数（保留一位小数）
            string kvString = kvRaw.ToString("D4");
            
            // 填充KV值
            for (int i = 0; i < 4; i++)
            {
                command[3 + i] = (byte)kvString[i];
            }
            
            command[7] = (byte)ProtocolConstants.CR;   // 结束字节
            
            return command;
        }
    }
}
